package com.elric.mall.controller;

import com.elric.mall.common.api.BaseController;
import com.elric.mall.common.api.CommonPage;
import com.elric.mall.common.api.CommonResult;
import com.elric.mall.common.utils.DateUtils;
import com.elric.mall.common.utils.ExcelFileUtil;
import com.elric.mall.dto.*;
import com.elric.mall.enums.ImportType;
import com.elric.mall.model.PlanProject;
import com.elric.mall.model.UmsAdmin;
import com.elric.mall.service.PlanProjectService;
import com.elric.mall.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 实现功能：规划项目管理接口
 *
 * @author 作者[Yuxuan.he]
 * @version 1.0.00  2021-09-16 10:44
 */
@Api(tags = "PlanProjectController", description = "规划项目管理接口")
@RequestMapping("/planProject")
@RestController
public class PlanProjectController extends BaseController {

    @Autowired
    PlanProjectService planProjectService;

    @Autowired
    private UserService userService;

    @ApiOperation("添加数据")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody PlanProject planProject, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        int count = planProjectService.create(planProject, umsAdmin);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改后台菜单")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(@RequestBody PlanProject planProject) {
        int count = planProjectService.update(planProject);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("保存项目信息")
    @RequestMapping(value = "/saveProjectInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult saveProjectInfo(@RequestBody PlanProjectDto dto, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        return CommonResult.success(planProjectService.save(dto, umsAdmin));
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PlanProject> getItem(@PathVariable String id) {
        PlanProjectDto dto = planProjectService.findById(id);
        return CommonResult.success(dto);
    }

    @ApiOperation("根据ID获取关联计划项目信息")
    @RequestMapping(value = "/getProgrammeById", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult getProgrammeById(@RequestParam(value = "id")  String id) {
        List<ProgrammeManageDto> dto = planProjectService.getProgrammeById(id);
        return CommonResult.success(dto);
    }

    @ApiOperation("根据ID删除数据")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable String id) {
        int count = planProjectService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询数据")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<PlanProject>> list(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestBody PlanProject planProject) {
        List<PlanProject> menuList = planProjectService.list(pageSize, pageNum, planProject);
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    /**
     * excel导入
     *
     * @param request 请求参数
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/importDocuments", method = RequestMethod.POST)
    @ApiOperation(value = "固定表导入", notes = "固定表导入")
    public CommonResult importDocuments(MultipartHttpServletRequest request) {
        return CommonResult.success(planProjectService.importDocuments(request));
    }

    @ApiOperation("批量导入")
    @RequestMapping(value = "/insertBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                    @RequestBody List<PlanProject> list) {
        return CommonResult.success(planProjectService.insertBatch(list, type));
    }

    @ApiOperation("批量导入")
    @RequestMapping(value = "/insertExcelBatch", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult insertExcelBatch(@RequestParam(value = "importType", defaultValue = "1") ImportType type,
                                    @RequestBody List<PlanProjectImportDto> list, Principal principal) {
        if(principal==null){
            return CommonResult.unauthorized(null);
        }
        UmsAdmin umsAdmin = userService.getAdminByPrincipal(principal);
        return CommonResult.success(planProjectService.insertExcelBatch(list, umsAdmin, type));
    }


    @ApiOperation("获取导入模板")
    @RequestMapping(value = "/getImportTemplate", method = RequestMethod.GET)
    @ResponseBody
    public void getImportTemplate(HttpServletResponse response) {
        ExcelFileUtil.toExcel(response, new ArrayList<>(), "规划项目导入模板",new PlanProjectImportDto());
    }

    @ApiOperation("计划项目数据导出")
    @RequestMapping(value = "/exportProject", method = RequestMethod.POST)
    @ResponseBody
    public void exportProject(@RequestBody PlanProject planProject, HttpServletResponse response) {
        List<PlanProjectExportDto> list = planProjectService.findExportByModel(planProject);
        String fileName = "规划项目报表"+ DateUtils.formatDate(new Date(), DateUtils.FORMAT_YYYYMMDDHH);
        ExcelFileUtil.toExcelWithXlsx(response, list, fileName,new PlanProjectExportDto());
    }
}
